package com.zhongxi.guoyuan.adapter.personCenter;

import android.content.Context;
import android.os.Build;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.coinStore.AttrsSelect;
import com.zhongxi.guoyuan.entity.coinStore.SelcetPostCartGoodsAttrs;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.DataUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/11/1.
 */

public class ExchangeItemOrderAdapter extends BaseAdapter {

    private int payType;
    private AttrsSelect attrs;
    private List<SelcetPostCartGoodsAttrs> goodsAttrses;
    private Context context;

    public ExchangeItemOrderAdapter(Context context, List<SelcetPostCartGoodsAttrs> goodsAttrses, AttrsSelect attrs, int payType) {
        this.context = context;
        this.goodsAttrses = goodsAttrses;
        this.attrs = attrs;
        this.payType = payType;
    }

    @Override
    public int getCount() {
        if (goodsAttrses != null && goodsAttrses.size() > 0) {
            return goodsAttrses.size();
        } else if (attrs != null && !"".equals(attrs)) {
            return 1;
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return goodsAttrses.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view == null) {
            vh = new ViewHolder();
            view = View.inflate(context, R.layout.exchange_item_sure_pay, null);
            vh.ll_item = (LinearLayout) view.findViewById(R.id.ll_item);
            vh.rl_item_cart = (RelativeLayout) view.findViewById(R.id.rl_item_cart);
            vh.ivImg = (ImageView) view.findViewById(R.id.ivImg);
            vh.tvName = (TextView) view.findViewById(R.id.tvName);
            vh.tvNum = (TextView) view.findViewById(R.id.tvNum);
            vh.tvAttr = (TextView) view.findViewById(R.id.tvAttr);
            vh.tvCash = (TextView) view.findViewById(R.id.tvCash);
            vh.tvReadBean= (TextView) view.findViewById(R.id.tvRedbean);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }

        if (goodsAttrses != null && goodsAttrses.size() > 0) {
            SelcetPostCartGoodsAttrs goodsAttrs = goodsAttrses.get(i);
            Glide.with(context).load(Url.IMG_URL + goodsAttrs.getImgUrl()).placeholder(R.mipmap.iv_eg).fitCenter().into(vh.ivImg);
            if (goodsAttrs.getName() != null && !"".equals(goodsAttrs.getName())) {
                vh.tvName.setText(goodsAttrs.getName().trim());
            }
            if (goodsAttrs.getGoodsnum() != null && !"".equals(goodsAttrs.getGoodsnum())) {
                vh.tvNum.setText("x " + goodsAttrs.getGoodsnum());
            }

            if (goodsAttrs.getItemGoodAttrs() != null && goodsAttrs.getItemGoodAttrs().size() > 0) {
                String str = "";
                for (int j = 0; j < goodsAttrs.getItemGoodAttrs().size(); j++) {
                    str += goodsAttrs.getItemGoodAttrs().get(j).getAttrv() + "/";
                }
                vh.tvAttr.setText("规格:" + str.substring(0, str.length() - 1));
            }

        } else {
            Glide.with(context).load(Url.IMG_URL + attrs.getThumbnail()).placeholder(R.mipmap.iv_eg).fitCenter().into(vh.ivImg);
            if (attrs.getName() != null && !"".equals(attrs.getName())) {
                vh.tvName.setText(attrs.getName());
            } else {
                vh.tvName.setText("");
            }
            if (attrs.getGoodsnum() != null && !"".equals(attrs.getGoodsnum())) {
                vh.tvNum.setText("x " + attrs.getGoodsnum());
            }

            if (attrs.getItemGoodAttrs() != null && attrs.getItemGoodAttrs().size() > 0) {
                String str = "";
                for (String attr : attrs.getItemGoodAttrs()) {
                    str += attr + "/";
                }
                vh.tvAttr.setText("规格:" + str.substring(0, str.length() - 1));
            }

            Glide.with(context).load(Url.IMG_URL + attrs.getThumbnail()).placeholder(R.mipmap.iv_eg).fitCenter().into(vh.ivImg);
            if (attrs.getGoodsnum() != null && !"".equals(attrs.getGoodsnum())) {
                vh.tvNum.setText("x" + attrs.getGoodsnum());
            }
            if (attrs.getPrice() != null && !"0".equals(attrs.getPrice())&&attrs.getRedprice()!=null&&!"0".equals(attrs.getRedprice())) {
                vh.tvCash.setText(DataUtil.multiplys(attrs.getPrice(),attrs.getGoodsnum(),2) + " 美豆");
                vh.tvReadBean.setText("+"+DataUtil.multiplys(attrs.getRedprice(),attrs.getGoodsnum(),2) + " 红豆");
            }else if ( attrs.getPrice() != null && !"0".equals(attrs.getPrice())&& attrs.getRedprice()!=null&&"0".equals(attrs.getRedprice())){
                vh.tvCash.setText(DataUtil.multiplys(attrs.getPrice(),attrs.getGoodsnum(),2) + " 美豆");
                vh.tvReadBean.setText("");
            }else if (attrs.getPrice() != null && "0".equals(attrs.getPrice())&& attrs.getRedprice()!=null&&!"0".equals(attrs.getRedprice())){
                vh.tvCash.setText("");
                vh.tvReadBean.setText(DataUtil.multiplys(attrs.getRedprice(),attrs.getGoodsnum(),2) + " 红豆");
            }
        }
        return view;
    }
    class ViewHolder {
        private LinearLayout ll_item;
        private RelativeLayout rl_item_cart;
        private ImageView ivImg;
        private TextView tvName;
        private TextView tvNum;
        private TextView tvAttr;
        private TextView tvCash;
        private TextView tvReadBean;
    }
}
